The typical sequence for running the MockServer docker image is as follows:

<ol>
	<li><a href="#install_docker">Install Docker</a></li>
	<li><a href="#pull_docker_image">Pull (or Update) Image</a></li>
	<li><a href="#run_docker_container">Run Container</a></li>
</ol>

In addition it is possible to <a href="#docker_container_customization">customise how the container is run</a>.

<a name="install_docker" class="anchor" href="#install_docker">&nbsp;</a>

<h3>Install Docker</h3>

<p>To install Docker see the <a href="https://docs.docker.com/installation/">installation instructions</a>.</p>

<a name="pull_docker_image" class="anchor" href="#pull_docker_image">&nbsp;</a>

<h3>Pull MockServer Image</h3>

<p>To pull the MockServer Docker image use the pull command, as follows:</p>

<pre><code class="code">docker pull jamesdbloom/mockserver</code></pre>

<p>This is not strictly necessary as the image will be automatically pulled if it does not exist when the run command is used. However, using the pull command will ensure the latest version of the image is downloaded.</p>

<a name="run_docker_container" class="anchor" href="#run_docker_container">&nbsp;</a>

<h3>Run MockServer Container</h3>

<p>Then to run MockServer as a Docker container run the following command:</p>

<pre><code class="code">docker run -d -P jamesdbloom/mockserver</code></pre>

<p>The <strong>-P</strong> switch in this command tells Docker to map all ports exported by the MockServer container to dynamically allocated ports on the host machine.</p><p>To view information about the MockServer container, including which dynamic ports have been used run the following command:</p>

<pre><code class="code">docker ps</code></pre>

<a name="docker_container_customization" class="anchor" href="#docker_container_customization">&nbsp;</a>

<h3>Configure Port Mapping</h3>

<p>This MockServer docker container exports the following port:</p>

<ul>
	<li><strong>serverPort</strong> 1080</li>
</ul>

<p>To specify which ports (on the host machine) should be mapped to the MockServer docker container use the <strong>-p</strong> <span class="command_line_argument_placeholder">&lt;host port&gt;</span><strong>:</strong><span class="command_line_argument_placeholder">&lt;container port&gt;</span> option, as follows:</p>

<pre><code class="code">docker run -d -p <span class="command_line_argument_placeholder">&lt;serverPort&gt;</span>:<span class="numeric_literal">1080</span> jamesdbloom/mockserver</code></pre>

<p>For example:</p>

<pre><code class="code">docker run -d -p <span class="numeric_literal">1080</span>:<span class="numeric_literal">1080</span> jamesdbloom/mockserver</code></pre>

<h3>Modifying Default Command</h3>

<p>By default when the MockServer container runs it executes a bash script passing three command line options, as follows</p>

<pre><code class="code">/opt/mockserver/run_mockserver.sh -serverPort <span class="numeric_literal">1080</span> -logLevel INFO</code></pre>

<p>It is possible to pass alternative arguments to the entrypoint for the container, by appending arguments to the end of the run command, as follows:</p>

<pre><code class="code">docker run -d -p <span class="numeric_literal">1080</span>:<span class="numeric_literal">1080</span> jamesdbloom/mockserver <em>-serverPort <span class="numeric_literal">1080</span> -logLevel INFO</em></code></pre>

<p>For following command can be used to view the available command line switches:</p>

<pre><code class="code"><strong>docker run jamesdbloom/mockserver ""</strong>

   Error: At least 'serverPort' must be provided

   run_mockserver.sh -serverPort <span class="command_line_argument_placeholder">&lt;port&gt;</span> [-proxyRemotePort <span class="command_line_argument_placeholder">&lt;port&gt;</span>] [-proxyRemoteHost <span class="command_line_argument_placeholder">&lt;hostname&gt;</span>] [-logLevel <span class="command_line_argument_placeholder">&lt;level&gt;</span>] [-jvmOptions <span class="command_line_argument_placeholder">&lt;system parameters&gt;</span>]

     valid options are:

        -serverPort <span class="command_line_argument_placeholder">&lt;port&gt;</span>                      The HTTP, HTTPS, SOCKS and HTTP CONNECT
                                                port(s) for both mocking and proxying
                                                requests.  Port unification is used to
                                                support all protocols for proxying and
                                                mocking on the same port(s). Supports
                                                comma separated list for binding to
                                                multiple ports.

        -proxyRemotePort <span class="command_line_argument_placeholder">&lt;port&gt;</span>                 Optionally enables port forwarding mode.
                                                When specified all requests received will
                                                be forwarded to the specified port, unless
                                                they match an expectation.

        -proxyRemoteHost <span class="command_line_argument_placeholder">&lt;hostname&gt;</span>             Specified the host to forward all proxy
                                                requests to when port forwarding mode has
                                                been enabled using the proxyRemotePort
                                                option.  This setting is ignored unless
                                                proxyRemotePort has been specified. If no
                                                value is provided for proxyRemoteHost when
                                                proxyRemotePort has been specified,
                                                proxyRemoteHost will default to \"localhost\".

        -logLevel <span class="command_line_argument_placeholder">&lt;level&gt;</span>                       Optionally specify log level as TRACE, DEBUG,
                                                INFO, WARN, ERROR or OFF. If not specified
                                                default is INFO

        -jvmOptions <span class="command_line_argument_placeholder">&lt;system parameters&gt;</span>  Specified generic JVM options or system properties.

   i.e. /opt/mockserver/run_mockserver.sh -serverPort 1080,1081 -proxyRemotePort 80 -proxyRemoteHost www.mock-server.com -jvmOptions -logLevel INFO "-Dmockserver.enableCORSForAllResponses=true -Dmockserver.sslSubjectAlternativeNameDomains='org.mock-server.com,mock-server.com'"</code></pre>

<p>Then the appropriate options can be specified, for example, to setup a port forwarding proxy (from <strong>0.0.0.0:1080</strong> to <strong>www.mock-server.com:80</strong>) using the following command:</p>

<pre><code class="code">docker run -d -p <span class="numeric_literal">1080</span>:<span class="numeric_literal">1080</span> jamesdbloom/mockserver -serverPort <span class="numeric_literal">1080</span> -proxyRemotePort <span class="numeric_literal">80</span> -proxyRemoteHost www.mock-server.com</code></pre>

<h3>Interactive Shell</h3>

<p>It is possible to launch the container with an interactive bash shell by modiying the entrypoint as follows:</p>

<pre><code class="code">docker run -it -p <span class="numeric_literal">1080</span>:<span class="numeric_literal">1080</span> --entrypoint <em>"/bin/bash"</em> jamesdbloom/mockserver</code></pre>

<p><strong>Note:</strong> in this example above the <strong>-d</strong> flag (for daemon) has been replaced with <strong>-i</strong> (to stdin open) and <strong>-t</strong> (for pseudo-tty) to ensure docker creates the container in the foreground with an attached stdin, see the <a href="https://docs.docker.com/reference/run/">docker documentation</a> for more details.</p>
